package com.ruoyi.lvlian.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.lvlian.domain.BusinessLicenseInfo;
import com.ruoyi.lvlian.domain.DriverInfo;
import com.ruoyi.lvlian.dto.ContractInfoDto;
import com.ruoyi.lvlian.util.ContactNumGenerator;
import com.ruoyi.lvlian.util.ExcelUtil;
import com.ruoyi.lvlian.util.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lvlian.mapper.ContractInfoMapper;
import com.ruoyi.lvlian.domain.ContractInfo;
import com.ruoyi.lvlian.service.IContractInfoService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合同信息Service业务层处理
 *
 * @author liyuxi
 * @date 2024-07-22
 */
@Service
public class ContractInfoServiceImpl implements IContractInfoService {
    @Autowired
    private ContractInfoMapper contractInfoMapper;

    /**
     * 查询合同信息
     *
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public ContractInfo selectContractInfoById(String id) {
        return contractInfoMapper.selectContractInfoById(id);
    }

    /**
     * 查询合同信息列表
     *
     * @param contractInfo 合同信息
     * @return 合同信息
     */
    @Override
    public List<ContractInfo> selectContractInfoList(ContractInfoDto contractInfo) {

        if (StringUtils.isNotEmpty(contractInfo.getInvoiceNum())) {
            return contractInfoMapper.queryContractInfoListByInvoiceNum(contractInfo);
        }

        return contractInfoMapper.selectContractInfoList(contractInfo);
    }

    /**
     * 新增合同信息
     *
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Override
    public int insertContractInfo(ContractInfo contractInfo) {

        contractInfo.setContractNum(ContactNumGenerator.generateSerialNumber(contractInfo.getBuyerCompanyName(), contractInfo.getSellerCompanyName()));
        contractInfo.setId(MD5Utils.md5(contractInfo.getContractNum()));
        contractInfo.setCreateUser(SecurityUtils.getUsername());
        contractInfo.setCreateDate(DateUtils.getCurrentTime());
        return contractInfoMapper.insertContractInfo(contractInfo);
    }

    /**
     * 修改合同信息
     *
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Override
    public int updateContractInfo(ContractInfo contractInfo) {

        int updateResult = contractInfoMapper.updateContractInfo(contractInfo);

        if (0 == updateResult) {
            updateResult = updateResult = this.insertContractInfo(contractInfo);
        }

        return updateResult;

    }

    /**
     * 批量删除合同信息
     *
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractInfoByIds(String[] ids) {
        return contractInfoMapper.deleteContractInfoByIds(ids);
    }

    /**
     * 删除合同信息信息
     *
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractInfoById(String id) {
        return contractInfoMapper.deleteContractInfoById(id);
    }

    @Override
    public List<ContractInfo> importInfo(MultipartFile file, String sheetName, int rowNum) throws Exception {
        byte[] fileBytes = file.getBytes();
        List<ContractInfo> invoiceRecyclingInfos = ExcelUtil.readExcel(fileBytes, ContractInfo.class, sheetName, rowNum);
        return invoiceRecyclingInfos;
    }

    @Override
    public int bachSave(List<ContractInfo> dtoList) {
        List<ContractInfo> newList = new ArrayList<>();
        int saveBatch = 0;
        if (!dtoList.isEmpty()) {
            for (ContractInfo po : dtoList) {
                if (StringUtils.isNotEmpty(po.getContractNum())) {
                    po.setId(MD5Utils.md5(po.getContractNum()));
                    po.setContractStatus(null);

                    po.setCreateUser(SecurityUtils.getUsername());
                    po.setCreateDate(DateUtils.getCurrentTime());
                    newList.add(po);
                }
            }

            String[] arr = newList.stream()
                    .map(ContractInfo::getContractNum) // 提取出 invoiceNum
                    .distinct()                        // 去重
                    .toArray(String[]::new);

//             导入之前 先删除
            contractInfoMapper.batchDelete(arr);

            saveBatch = contractInfoMapper.batchSave(newList);
        }
        return saveBatch;
    }
}
